<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button style="float:right" type="primary" @click="initdata()" size="small">查询搜索</el-button>
        <el-button style="float:right;margin-right: 15px" @click="resetfiled()" size="small">重置</el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="设备名称">
            <el-input v-model="listQuery.name" class="input-width" placeholder="请输入设备名称" clearable></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button size="mini" class="btn-add" @click="isadd()" style="margin-left: 20px">添加</el-button>
    </el-card>

    <div style="margin:0px;" v-loading="loading">
      <el-table :data="tableData" border stripe>
        <el-table-column prop="name" label="设备名称" align="center"></el-table-column>
        <el-table-column prop="moId" label="所属管理处ID" align="center"></el-table-column>
        <el-table-column prop="sectionId" label="所属路段ID" align="center"></el-table-column>
        <el-table-column prop="categoryId" label="设备所属分类ID" align="center"></el-table-column>
        <el-table-column prop label="上下行方向" align="center">
          <template slot-scope="scope">{{ scope.row.isUp | upstr }}</template>
        </el-table-column>
        <el-table-column prop="isRoadEquip" label="是否路上设备" align="center">
          <template slot-scope="scope">{{ scope.row.isRoadEquip | roadstr }}</template>
        </el-table-column>
        <el-table-column prop="status" label="设备状态" align="center">
          <template slot-scope="scope">{{ scope.row.status | statusstr }}</template>
        </el-table-column>
        <el-table-column prop="pileNo" label="桩号" align="center"></el-table-column>
        <el-table-column prop="location" label="经纬度" align="center"></el-table-column>

        <el-table-column label="操作" align="center" width="180">
          <template slot-scope="scope">
            <!-- <el-button type="primary" @click="delrecord(scope.row)">附加属性</el-button> -->
            <el-button type="primary" @click="delrecord(scope.row)">删除</el-button>
            <el-button type="primary" @click="showupdaterecord(scope)">更新</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="text-align:center;">
      <el-pagination
        @size-change="sizeChange"
        @current-change="currentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalNum"
        background
      ></el-pagination>
    </div>
    <el-dialog
      :title="isupdate ? '更新' : '添加'"
      :visible.sync="isadddia"
      center
      width="40%"
      @close="isadddia = false"
    >
      <el-form
        :model="refform"
        ref="refform"
        label-width="120px"
        :rules="rules"
        style="margin: 0px 70px;"
      >
        <el-form-item label="设备名称" prop="name">
          <el-input v-model="refform.name"></el-input>
        </el-form-item>
        <el-form-item label="所属管理处" prop="moId">
          <!-- <el-input v-model="refform.moId"></el-input> -->
          <el-select v-model="refform.moId" placeholder="请选择">
            <el-option
              v-for="item in manageoptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属路段" prop="sectionId">
          <!-- <el-input v-model="refform.sectionId"></el-input> -->
          <el-select v-model="refform.sectionId" placeholder="请选择">
            <el-option
              v-for="item in roadoptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备所属分类" prop="categoryId">
          <!-- <el-input v-model="refform.categoryId"></el-input> -->
          <el-select v-model="refform.categoryId" @change="devTypeChange" placeholder="请选择">
            <el-option
              v-for="item in devtypeoptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上下行方向" prop>
          <!-- <el-input v-model="refform.isUp"></el-input> -->
          <el-radio-group v-model="isUp">
            <el-radio :label="1">上行</el-radio>
            <el-radio :label="0">下行</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否路上设备" prop>
          <!-- <el-input v-model="refform.isRoadEquip"></el-input> -->
          <el-radio-group v-model="isRoadEquip">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
            <!-- <elradiom v-model="refform.isRoadEquip" :label="1">是</elradiom>
            <elradiom v-model="refform.isRoadEquip" :label="0">否</elradiom>-->
          </el-radio-group>
        </el-form-item>
        <el-form-item label="设备状态" prop>
          <!-- <el-input v-model="refform.status"></el-input> -->
          <el-radio-group v-model="status">
            <el-radio :label="-1">离线</el-radio>
            <el-radio :label="1">停机</el-radio>
            <el-radio :label="2">故障</el-radio>
            <el-radio :label="3">在线</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="桩号" prop="pileNo">
          <el-input v-model="refform.pileNo"></el-input>
        </el-form-item>
        <el-form-item label="经纬度" prop="location">
          <el-input v-model="refform.location"></el-input>
        </el-form-item>

        <el-table :data="refform.attrs" border stripe v-if="ifattrs">
          <el-table-column prop="name" label="标签名" width></el-table-column>
          <el-table-column label="标签值" width>
            <template slot-scope="scope">
              <el-input v-model="scope.row.value" label></el-input>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <span slot="footer">
        <el-button @click="isadddia = false">取 消</el-button>
        <el-button type="primary" @click="isupdate ? updaterecord() : addrecord()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import devService from "@/api/devService";
import devTypeService from "@/api/devTypeService";
import roadService from "@/api/roadService";
import managementService from "@/api/managementService";
import elradiom from "./el-radiom.vue";

export default {
  components: { elradiom },
  data() {
    return {
      loading: true,
      manageoptions: [],
      roadoptions: [],
      devtypeoptions: [],
      listQuery: {
        name: ""
      },
      pageSize: 5,
      currentPage: 1,
      totalNum: 0,
      isupdate: false,
      isshowqr: false,
      qrdata: "",
      tableData: [],
      isadddia: false,
      refform: {
        name: "",
        moId: "",
        sectionId: "",
        categoryId: "",
        pileNo: "",
        location: "",
        attrs: []
      },
      isUp: 1,
      isRoadEquip: 1,
      status: 3,
      rules: {
        name: [{ required: true, message: "不能为空", trigger: "blur" }],
        pileNo: [{ required: true, message: "不能为空", trigger: "blur" }],
        location: [{ required: true, message: "不能为空", trigger: "blur" }],
        moId: [{ required: true, message: "不能为空", trigger: "change" }],
        sectionId: [{ required: true, message: "不能为空", trigger: "change" }],
        categoryId: [{ required: true, message: "不能为空", trigger: "change" }]
      },
      ifattrs: true
    };
  },
  computed: {},
  watch: {
    isupdate: {
      handler: function(n, o) {
        if (n) {
        }
      }
    }
  },
  mounted() {
    this.initdata();
  },

  filters: {
    upstr(val) {
      switch (val) {
        case 1:
          return "上行";
          break;
        case 0:
          return "下行";
          break;
        default:
          break;
      }
    },
    roadstr(val) {
      switch (val) {
        case 1:
          return "是";
          break;
        case 0:
          return "否";
          break;
        default:
          break;
      }
    },
    statusstr(val) {
      switch (val) {
        case -1:
          return "离线";
          break;
        case 1:
          return "停机";
          break;
        case 2:
          return "故障";
          break;
        case 3:
          return "在线";
          break;
        default:
          break;
      }
    }
  },
  methods: {
    showupdaterecord(scope) {
      this.isupdate = true;
      this.refform = _.cloneDeep(scope.row);
      this.isadddia = true;
    },
    devTypeChange(val) {
      this.devtypeoptions.map(item => {
        if (item.id == val) {
          this.refform.attrs = [];
          item.attrs.map((e, index) => {
            //使用vue set 替换
            //使用 v-if 加判断
            this.refform.attrs.push(e);
            this.$set(this.refform.attrs[index], "value", e.defaultValue);
            // this.ifattrs = false;
            // this.$nextTick(function() {
            //   this.ifattrs = true;
            // });
          });
        }
      });
      // this.refform.attrs = selectdevtype.attrs;
    },
    addattr() {
      let attr = {
        customTag: "",
        name: "",
        value: ""
      };
      this.refform.attrs.push(attr);
    },
    delattr(scope) {
      this.refform.attrs.splice(scope.$index, 1);
    },
    sizeChange(val) {
      this.pageSize = val;
      this.initdata();
    },
    currentChange(val) {
      this.currentPage = val;
      this.initdata();
    },

    delrecord(row) {
      devService.del({ ids: row.id }).then(res => {
        this.initdata();
        this.$message.success("删除成功");
      });
    },
    updaterecord() {
      this.$refs.refform.validate(valid => {
        if (valid) {
          this.refform.isUp = this.isUp;
          this.refform.isRoadEquip = this.isRoadEquip;
          this.refform.status = this.status;
          devService.update(this.refform).then(res => {
            this.isadddia = false;
            this.initdata();
            this.$message.success("修改成功");
          });
        }
      });
    },
    initdata() {
      //获取设备列表
      this.loading = true;
      devService
        .select({
          page: this.currentPage,
          count: this.pageSize,
          name: this.listQuery.name,
          isAttr: 1
        })
        .then(res => {
          this.tableData = res.data.records;
          this.totalNum = res.data.total;
          // this.$message.success("数据加载成功");
          this.loading = false;
        });
      //获取设备分类
      devTypeService
        .select({
          page: 1,
          count: 1000,
          isAttr: 1
        })
        .then(res => {
          this.devtypeoptions = res.data.records;
        });
      //获取路段列表
      roadService
        .select({
          page: 1,
          count: 1000
        })
        .then(res => {
          this.roadoptions = res.data.records;
        });
      //获取管理处列表
      managementService
        .select({
          page: 1,
          count: 1000
        })
        .then(res => {
          this.manageoptions = res.data.records;
        });
    },
    isadd() {
      this.isadddia = true;
      this.isupdate = false;
      this.resetinputdia();
    },

    resetinputdia() {
      this.refform = {
        name: "",
        moId: "",
        sectionId: "",
        categoryId: "",
        isUp: "",
        isRoadEquip: "",
        status: "",
        pileNo: "",
        location: "",
        attrs: []
      };
    },

    resetfiled() {
      this.listQuery.name = "";
    },

    addrecord() {
      // this.isadddia = true
      //
      this.$refs.refform.validate(valid => {
        if (valid) {
          this.refform.isUp = this.isUp;
          this.refform.isRoadEquip = this.isRoadEquip;
          this.refform.status = this.status;
          devService.add(this.refform).then(res => {
            this.isadddia = false;
            this.initdata();
            this.$message.success("添加成功");
          });
        }
      });
    }
  }
};
</script>
<style lang='scss' scoped>
</style>